package com.hiwhitley.easy.day09;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by hiwhitley on 2016/8/13.
 */
public class Intersect {

    public static int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        List<Integer> temp = new ArrayList<Integer>();
        for (int i = 0, j = 0; i < nums1.length && j < nums2.length;){
            if (nums1[i] < nums2[j]) i++;
            else if (nums1[i] > nums2[j]) j++;
            else {
                temp.add(nums1[i]);
                i++; j++;
            }
        }
        int[] res = new int[temp.size()];
        for (int i = 0; i < temp.size(); i++)
            res[i] = temp.get(i);
        return res;
    }
    public static void main(String[] args) {
        System.out.println(intersect(new int[]{1, 2, 2, 1}, new int[]{2}));
    }
}
